#!/bin/bash

awk 'BEGIN{
	FS 		 =",";
	OFS 	 =",";
	power1[""] ="";
	power2[""] ="";
	current1[""]="";
	current2[""]="";
	count[""]=0;
	
}{
	if($1 ~/.*YZ.*8/)
	{
#		print $0;
		split($2,tarray," ");
		split(tarray[1],dayarray,"/");
		split(tarray[2],harray,":");
		if(tarray[3]=="PM")
			harray[1]=harray[1]+12;

		time=mktime(dayarray[3]" "dayarray[1]" "dayarray[2]" "harray[1]" "harray[2]" "harray[3]);	
		
		if($1 ~/.*PS1.*Power8/)
		{
			power1[time]=$3;
			count[time]++;
		}
		if($1 ~/.*PS3.*Power8/)
		{
			power2[time]=$3;
			count[time]++;
		}	
		if($1 ~/.*PS1.*Current8/)
		{
			split($3,temp,"e");
			if(temp[2]=="-001")
		         temp[1]=temp[1]*0.1;
		    current1[time]=temp[1];	
		    count[time]++;
		}
		
		if($1 ~/.*PS3.*Current8/)
		{
			split($3,temp,"e");
			if(temp[2]=="-001")
		         temp[1]=temp[1]*0.1;
		    current2[time]=temp[1];		
		    count[time]++;			
		}	

	}
	
}END{
		for(i in count)
			printf("%10s,%5d,%5d,%5d,%.2f,%.2f\n", i, power1[i],power2[i],power1[i]+power2[i],current1[i],current2[i]);

	
}' $1 |sort -n -k1,1n  >> powercurrent
